package com.hx.er.action.manager.user;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.hx.er.action.base.ErManagerAction;
import com.hx.er.common.entity.DateIntervalEntity;
import com.hx.er.common.entity.LoginInfoEntity;
import com.hx.er.common.system.ErRestraurant;
import com.hx.er.common.system.ErSessions;
import com.hx.er.common.system.ErSystem;
import com.hx.er.service.manager.IManagerUserService;

public class ManagerHomeAction extends ErManagerAction {
	private static final long serialVersionUID = 4926512300151605899L;

	private int userCount;
	private int userLoginCount;
	private long maxMemory;
	private long totalMemory;
	private long freeMemory;
	private int restCount;
	private DateIntervalEntity startDateInterval;
	private int msgCount0;
	private int msgCount1;
	private int msgCount8;
	private Date lastLoginDate;
	private Date currentLoginDate;
	private List<String> statLabelList;
	private String statString;

	@Autowired
	private IManagerUserService managerUserService;

	public String init() {
		// 获取已登录人数，通过session实现
		this.userLoginCount = ErSessions.getUserLoginCount();
		// 最大内存
		this.maxMemory = Runtime.getRuntime().maxMemory() / 1024 / 1024;
		// 内存总量
		this.totalMemory = Runtime.getRuntime().totalMemory() / 1024 / 1024;
		// 空闲内存
		this.freeMemory = Runtime.getRuntime().freeMemory() / 1024 / 1024;
		// 网站个数
		this.restCount = ErRestraurant.getCount();
		// 用户总数
		this.userCount = this.managerUserService.getUserDetailCount(null);
		// 系统运行时间
		this.startDateInterval = ErSystem.getSysRunningTimeLenDate();
		// 消息数
		Map<String, String> conditions = new HashMap<String, String>();
		conditions.put("restId", String.valueOf(super.restId));
		List<Map<Integer, Integer>> msgCountList = this.managerUserService.getFlowStatusCount(conditions);
		for (Map<Integer, Integer> map : msgCountList) {
			if (map.get("flowStatusId") == 0) {
				this.msgCount0 = map.get("count");
			} else if (map.get("flowStatusId") == 1) {
				this.msgCount1 = map.get("count");
			} else if (map.get("flowStatusId") == 8) {
				this.msgCount8 = map.get("count");
			}
		}
		// 查询登录时间
		LoginInfoEntity loginInfo = new LoginInfoEntity();
		loginInfo.setUserId(super.userInfoLogin.getUserId());
		List<LoginInfoEntity> loginInfoList = this.managerUserService.getLoginInfoList(loginInfo);
		if (loginInfoList.size() > 0) {
			this.currentLoginDate = loginInfoList.get(0).getLoginDate();
		}
		if (loginInfoList.size() > 1) {
			this.lastLoginDate = loginInfoList.get(1).getLoginDate();
		}
		
		return SUCCESS;
	}

	public int getUserLoginCount() {
		return userLoginCount;
	}

	public long getMaxMemory() {
		return maxMemory;
	}

	public long getTotalMemory() {
		return totalMemory;
	}

	public long getFreeMemory() {
		return freeMemory;
	}

	public int getRestCount() {
		return restCount;
	}

	public int getUserCount() {
		return userCount;
	}

	public DateIntervalEntity getStartDateInterval() {
		return startDateInterval;
	}

	public Integer getMsgCount0() {
		return msgCount0;
	}

	public Integer getMsgCount1() {
		return msgCount1;
	}

	public Integer getMsgCount8() {
		return msgCount8;
	}

	public Date getCurrentLoginDate() {
		return currentLoginDate;
	}

	public List<String> getStatLabelList() {
		return statLabelList;
	}

	public String getStatString() {
		return statString;
	}

	public Date getLastLoginDate() {
		return lastLoginDate;
	}

}
